<?php
namespace Mentors\Controller;

class PlanController extends MainController {

    public function index(){
        $status = I('get.status');
        $obj = M('Plan');
        $map['mentorsID']=$this->userID;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 12);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            unset($map);
            $map['id']=$value['trainID'];
            $list[$key]['trainName']=M("Train")->where($map)->getField("trainItem");
          }  
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }  

    //创建培训计划
    public function create(){
        $trainID = I('get.trainID');
        $map['id'] = $trainID;
        $map['mentorsID'] = $this->userID;
        $train = M('Train')->where($map)->find();
        if (!$train) {
            $this->error(L('_public_noinfo'));
        }
        unset($map);
        $map['trainID'] = $trainID;
        $list = M('Plan')->where($map)->find();
        if ($list) {
            redirect(U('Plan/node',array('planID'=>$list['id'],'trainID'=>$trainID)));
        }
        $mentees = M('Mentees')->where('userid='.$train['menteesID'])->find();
        $Mentors = M('Mentors')->where('userid='.$train['mentorsID'])->find();
        $this->assign('mentees',$mentees);
        $this->assign('Mentors',$Mentors);
        $this->assign('train',$train);

        $this->display();
    }

    //保存计划
    public function save(){
        $obj = M('Plan');
        $map['trainID'] = I('post.trainID');
        $list = $obj->where($map)->find();
        if ($list) {
            $state = "Please don't repeat";
        }else{
            $obj = D('Plan');
            if ($data = $obj->create()) {
                if ($list = $obj->add($data)) {
                    $state = 'SUCCESS';
                    $message = L('_action_success');
                    $url = U('Plan/node',array('planID'=>$list,'trainID'=>$data['trainID']));
                } else {
                    $state = L('_action_success');
                }
            } else {
                $state = $obj->getError();
            }
        }        
        echo $this->echo_json_str($state,$message,$url);
    }

    //编辑计划
    public function edit(){
        $id = I('get.id');
        $map['id'] = $id;
        $map['mentorsID'] = $this->userID;    
        $plan = M('Plan')->where($map)->find();
        if (!$plan) {
            $this->error(L('_public_noinfo'));
        }else{
            $list = M('PlanDetails')->where('planID='.$id)->order('startTime desc')->select();
            $this->assign('list',$list);  
            $this->assign('plan',$plan); 

            $mentees = M('Mentees')->where('userid='.$plan['menteesID'])->find();
            $this->assign('mentees',$mentees);

            unset($map);
            $map['id'] = $plan['trainID'];
            $map['mentorsID'] = $this->userID;
            $train = M('Train')->where($map)->find();
            $this->assign('train',$train);

            $this->display();
        }
    }

    //更新计划
    public function update(){
        $url = U('Plan/node',array('planID'=>I('post.id'),'trainID'=>I('post.trainID')));
        $this->all_save('Plan',$url);
    }

    //删除计划
    public function del(){
        $id = I('get.id');
        $map['id'] = $id;
        $map['mentorsID'] = $this->userID;
        M('Plan')->where($map)->delete();
        unset($map);
        $map['planID'] = $id;
        $map['mentorsID'] = $this->userID; 
        M('PlanDetails')->where($map)->delete();
        redirect(U('Plan/index'));
    }

    public function node(){
        $planID = I('get.planID');
        $trainID = I('get.trainID');
        $map['id'] = $planID;
        $plan = M('Plan')->where($map)->find();
        if (!$plan) {
            redirect(U('Plan/create',array('trainID'=>$trainID)));
        }

        $mentees = M('Mentees')->where('userid='.$plan['menteesID'])->find();
        $mentees['trainItem'] = M('Train')->where('id='.$plan['trainID'])->getField('trainItem');
        $this->assign('mentees',$mentees);
        $this->assign('plan',$plan);

        $list = M('PlanDetails')->where('planID='.$planID)->order('startTime desc')->select();
        $this->assign('list',$list);  
        $this->display();
    }

    //添加节点
    public function addnode(){
        $planID = I('get.planID');        
        $this->assign('planID',$planID);        
        $this->display();
    }

    //保存节点
    public function savenode(){
        $this->all_add('PlanDetails');
    }
    //编辑节点
    public function editnode(){
        $id = I('get.id');
        $map['mentorsID'] = $this->userID;
        $map['id'] = $id;
        $list = M('PlanDetails')->where($map)->find();
        if (!$list) {
            $this->error(L('_public_noinfo'));
        }else{
            $this->assign('list',$list);
            $this->display(); 
        }        
    }
    //更新节点
    public function updatenode(){
        $this->all_save('PlanDetails');
    }
    //删除节点
    public function delnode(){
        $id = I('get.id');
        $map['id'] = $id;
        $map['mentorsID'] = $this->userID;    
        M('PlanDetails')->where($map)->delete();    
        redirect($_SERVER["HTTP_REFERER"]);
    }
    //编辑节点
    public function commentnode(){
        $id = I('get.id');
        $map['mentorsID'] = $this->userID;
        $map['id'] = $id;
        $list = M('PlanDetails')->where($map)->find();
        if (!$list) {
            $this->error(L('_public_noinfo'));
        }else{
            $this->assign('list',$list);
            $this->display(); 
        }        
    }
}